// pages/mall/proList/proList.js
import * as API_Mall_Product from '../../../api/mall/product'

Page({

  /**
   * 页面的初始数据
   */
  data: {
    title:'',
    classifyId:'',
    productList:[],
    isFinshed:false,
    params:{
      secondClassifyId:'',
      pageNum:1,
      pageSize:20,
      orderByColumn:'',//{"userName":"ascending","userId":"descending"}
    },
    sort:["","ascending","descending"],

    priceTemp:[{},{}],
    salesIndex:0,
    priceIndex:0,
    orderJso:{}
  },
  loadCartNum(){
    let guide = this.selectComponent("#cart")
    guide.loadCount()
   },
  changeOrderBy(e){
    //处理销量排序
    if(e.currentTarget.dataset.type == 'sales'){
      let sales = e.currentTarget.dataset.index
      if(sales == 2){
        sales = 0
        delete  this.data.orderJso['salesNum']
      }else{
        sales = sales+1
        this.data.orderJso['salesNum']=this.data.sort[sales];
      }
      this.setData({
        salesIndex:sales
      })
    }else{
      let price = e.currentTarget.dataset.index
      if(price == 2){
        price = 0
        delete this.data.orderJso['currentPrice']
      }else{
        price = price+1
      }
      this.data.orderJso['currentPrice']=this.data.sort[price]
      
      this.setData({
        priceIndex:price
      })
    }
    let str =JSON.stringify(this.data.orderJso)
    this.setData({
      productList:[],
      'params.pageNum':1,
      'params.orderByColumn':str,
    })
    this.loadData()
  },

  genStr(){
    // debugger
    var r = this.data.tempOrderBy.filter(function (s) {
        return s && s.trim();
    });
    let str = JSON.stringify(r);
    str = str.replace('[','{')
    return str.replace(']','}')
  },

  
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log(options)
    this.setData({
      title:options.name,
      'params.secondClassifyId':options.id
    })
    this.initGlobalData();
    this.loadData()
  },
  loadData(){
    API_Mall_Product.getList(this.data.params).then(res=>{
      let arr = this.data.productList
      arr.push(...res.rows)
      this.setData({
        productList:arr,
        isFinshed:res.lastPage
      })
    })
  },
  initGlobalData:function(){
    let globalData=getApp().globalData;
    let statusBarHeightpx=globalData.statusBarHeightpx;
    let navHeadHeightpx=globalData.navHeadHeightpx;
    let navHeightpx=globalData.navHeightpx;
    this.setData({navHeadHeightpx:navHeadHeightpx,navHeightpx:navHeightpx,statusBarHeightpx:statusBarHeightpx});
  },
  goDetail(e){
    wx.navigateTo({
      url: '../../mall/proDetail/proDetail?id='+e.currentTarget.dataset.id,
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
   this.loadCartNum()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    if(this.data.isFinshed)
      return

    let page = this.data.params.pageNum+1
    this.setData({
      'params.pageNum':page
    })
      this.loadData()
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})